# -*- coding: utf-8 -*-
# The cannon class

import pygame.transform

import data
import default
import sound
import bullet

class Cannon(pygame.sprite.Sprite):
    def __init__(self, pos,
                 direction="right", bullet_vel = (20, 0),
                 image = default.cannon_sprite,
                 coord_scale = 1):
        """Direction indicates in which direction to fire,
        bullet_vel is the absolute speed for the fired bullets horizontally and vertically,
        the sprite can be overridden with image argument"""
        pygame.sprite.Sprite.__init__(self)
        
        self.orig_image, self.rect = data.load_image(image, -1)

        self.direction = direction
        
        if self.direction == "left":
            #flip it so that it faces left
            self.image = pygame.transform.flip(self.orig_image, True, False)
            self.bullet_vel = (-bullet_vel[0], bullet_vel[1])
        else:
            self.image = self.orig_image
            self.bullet_vel = (bullet_vel[0], bullet_vel[1])
        
        #real coordinates, we keepp self.rect is used for drawing and such
        self.world_pos = pygame.Rect(pos, self.rect.size)
        self.world_pos.bottomleft = pos
        
        self.coord_scale = coord_scale
        
        self.time_to_next = 0

    def update(self, camera, curr_level = None, t=1):
        """Fire if player is in line of sight.
        Also prepare for drawing, by using camera to calculate screen coordinates"""

        player_pos = curr_level.player.world_pos

        if self.direction == "right":
            player_in_front_of = self.world_pos.right < player_pos.right
        else:
            player_in_front_of = player_pos.left < self.world_pos.left

            #if player.h > self.world.pos.h:
        if player_in_front_of:
            for point in (player_pos.top, player_pos.bottom, player_pos.centery):
                # bottom is inside line of sight
                # top is inside line of sight
                # line of sight intersects player
                if (self.world_pos.top < point and \
                    point < self.world_pos.bottom):# or \
                    #(self.world_pos.top < point and \
                    # point < self.world_pos.bottom) or \
                    #(self.world_pos.top < point and \
                    # point > self.world_pos.bottom):    
                    self.fire(curr_level)
                        
            if (player_pos.top < self.world_pos.top and \
                self.world_pos.bottom < player_pos.bottom):
                    self.fire(curr_level)

        # We need to convert the world corrdinates to screen coords, for drawing
        x = ((self.world_pos.x - camera.x))# / self.coord_scale)
        y = ((self.world_pos.y - camera.y))# / self.coord_scale)
        
        self.rect.topleft = (x, y)
        
        if self.time_to_next > 0:
            self.time_to_next -= 1
                
    def fire(self, curr_level):
        """Create a bullet and shove it away"""
        if not self.time_to_next > 0:
            sound.play_sound("select.wav")
            if self.direction == "left":
                origin = self.world_pos.midleft
            else:
                origin = self.world_pos.midright
            curr_level.damaging_group.add(bullet.Bullet(origin, self.bullet_vel, default.cannon_ball)) 
            self.time_to_next = default.frames_between_shots
